#include "monster.h"
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<stdbool.h>
#include<StdFile.h>

Monster * CreateMonster(const char* name,int HP,int attack,int defence,char* symbol,int exp,int Gold)
{
    Monster *m = (Monster*)malloc(sizeof(Monster));
    if(m == NULL)
        return NULL;
    m->HP = HP;
    m->symbol = symbol;
    m->attack = attack;
    m->defence = defence;
    m->name = GetString(name);
    m->x=0;
    m->y=0;
    m->exp = exp;
    m->Gold = Gold;
    return m;
}

void SetRandPoint(void* ptr)
{
    Monster *m =(Monster*)ptr;
    m->x = rand() % 10;
    m->y = rand() % 10;
    while(m->x==0 && m->y==0)
    {
        m->x = rand() % 10;
        m->y = rand() % 10;
    }
}

void FreeMonster(void *ptr)
{
    Monster *m = (Monster*)ptr;
    ClearString(&m->name);
    free(m);
}

void SetMonsters(DArray *ms)
{
    //随机种子
    srand(time(NULL));
   
    for(int i=0;i<3;i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("宝箱",1,0,0,"  🧰 ",0,0));
    }
    for(int i=0;i<8;i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("暗影狼",20,3,1,"  🐺 ",10,3));
    }
    for(int i=0;i<5;i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("丧尸",50,5,3,"  🧟 ",15,5));
    }
    for(int i=0;i<3;i++)
    {
        ms->InsertArrayTail(ms,CreateMonster("骷髅",80,8,5,"  💀 ",20,10));
    }
    ms->InsertArrayTail(ms,CreateMonster("恶魔撒旦",200,25,15,"  👿 ",0,0));
    ms->DisplayDynamicArray(ms,SetRandPoint);
}

static char*(*MyMap)[MAPSIZE]=NULL;

void SetPosition(void* ptr)
{
    Monster *m= (Monster*)ptr;
    MyMap[m->x][m->y] = m->symbol;
}

bool SortByPoint(void* ptr1,void* ptr2)
{
    Monster *m1 = (Monster*)ptr1;
    Monster *m2 = (Monster*)ptr2;
    if(m1->x > m2->x)
    {
        return true;
    }
    else if(m1->x == m2->x)
    {
        return m1->y > m2->y;
    }
    else
    {
        return false;
    }
}

void SetPosDumplicate(DArray *ms)   //位置去重      
{
    int flag=0;
    ms->Sort(ms,SortByPoint);
   for(int i=0;i < ms->len-1;i++)
   {
    Monster *m1 = (Monster*)ms->dp[i];
    Monster *m2 = (Monster*)ms->dp[i+1];

        if(m1->x == m2->x && m1->y == m2->y)
        {
            m2->y++;
            if(m2->y == MAPSIZE)
            {
                flag = 1;
                m2->x++;
                m2->x = m2->x % MAPSIZE;
                m2->y = 1;
            }
        }

        if(flag == 1)
        {
            SetPosDumplicate(ms);
        }
   }
}

void PrintMonster(void* ptr)
{
    Monster *m = (Monster*)ptr;
}

void SetMonstersPosition(DArray *ms, char*(* map)[MAPSIZE])
{
    //去重
    SetPosDumplicate(ms);

    //在地图上显示怪兽
    MyMap =map;
    ms->DisplayDynamicArray(ms,SetPosition);
    MyMap = NULL;
    ms->DisplayDynamicArray(ms,PrintMonster);

}

Monster * GetMonsterByPosition(DArray *ms, int x, int y)
{
    for(int i=0;i<ms->len;i++)
    {
        Monster *m1 = (Monster*)ms->dp[i];
        if(m1->x == x && m1->y == y && m1->HP > 0 )
        {
            return m1;
        }
    }
    return NULL;
}


MyStr MonsterToString(DArray *ms)
{
    char str[2048]={0};
    char MonsterList[2048]={0};
    for(int i=0;i<ms->len;i++)
    {
        Monster *m = (Monster*)ms->dp[i];
        sprintf(str,"%d %d %d %d %d %s %d %d %s\n",m->HP,m->attack,m->defence,m->x,m->y,m->name.string,m->exp,m->Gold,m->symbol);
        strcat(MonsterList,str);
    }
    return GetString(MonsterList);
} 


Monster* ReCreatMonster(int HP,int Attack,int Def,int X,int Y,const char* Name,int Exp,int Gold,const char* Symbol)
{
    Monster* m = (Monster*)malloc(sizeof(Monster));
    if(m==NULL)
    {
        return NULL;
    }
    m->HP = HP;                      //生命值
    m->attack = Attack;              //攻击力
    m->x = X;
    m->y = Y;
    m->symbol = GetString(Symbol).string;             //图标
    m->name = GetString(Name);      //怪物名
    m->exp = Exp;                   //持有经验
    m->Gold = Gold;                 //持有金币
    m->defence = Def;                   //防御力
    return m; 
}



/*从文件解析怪物字符串数组*/
void StringToMonster(MyStrList* monster,DArray* ms)
{
    //解析怪物数组之前首先对需要存入的数组初始化
    InitDynamicArray(ms);
    int TempHp = 0;
    int TempAttack = 0;
    int TempX = 0;
    int TempY = 0;
    
    char TempName[64] = {0};
    int TempExp = 0;
    int TempGold = 0;
    int TempDef = 0;
    
    for(int i=0;i<monster->len;i++)
    {   
        char* TempSymbol;
        char symbol[32];
        TempSymbol = symbol;
        char sym[32] ="  ";
                                //sprintf(str,"%d %d %d %d %d %s %d %d %s\n",m->HP,m->attack,m->defence,m->x,m->y,m->name,m->exp,m->Gold,m->symbol);

        sscanf(monster->stringlist[i].string,"%d %d %d %d %d %s %d %d %s",&TempHp,&TempAttack,&TempDef,&TempX,&TempY,TempName,&TempExp,&TempGold,TempSymbol);
        strcat(TempSymbol," ");
        strcat(sym,TempSymbol);

        ms->InsertArrayTail(ms,ReCreatMonster(TempHp,TempAttack,TempDef,TempX,TempY,TempName,TempExp,TempGold,sym));
    }
}


//从存档重塑怪物
